package com.lyh.hkcloud.module.m1300091;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lyh.hkcloud.R;
import com.lyh.hkcloud.base.BaseActivity;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.constants.HttpUrlComstants;
import com.lyh.hkcloud.module.m1300091.adapter.RecyclerViewAdapter;
import com.lyh.hkcloud.module.m1300091.service.Service;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.StringUtils;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wwl
 * @program: hkCloud
 * @description: 盘点资产信息
 * @company—date: 利元数据 2024-05-03
 **/
public class IndexActivity extends BaseActivity implements View.OnClickListener{

    private ImageView goBack;
    private Service service;
    private String inventoryPlanId;
    private String scanResultCode;
    private Button btYes;
    private Button btNo;
    private Button btJhw;

    private RecyclerView recyclerView;
    private List<Map<String,Object>> dataMap = new ArrayList<>();
    private RecyclerViewAdapter recyclerViewAdapter;
    List<String> assetIdList = new ArrayList<>();


    /**
     * 上一个页面传过来的 盘点id和盘点状态
     */
    private JSONObject data;

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected int getSubmitResId() {
        return R.id.bt_receiver;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_1300091;
    }

    @Override
    protected void onBeginInit() {
        service = new Service(IndexActivity.this);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView() {
        goBack = findViewById(R.id.go_back);
        recyclerView = findViewById(R.id.recyclerView);
        goBack.setOnClickListener(this);
        btYes=findViewById(R.id.bt_yes);
        btNo=findViewById(R.id.bt_no);
        btJhw = findViewById(R.id.bt_jhw);
        getSupportActionBar().hide();

        //获取盘点列表页面的参数
        try {
            data = new JSONObject(getIntent().getStringExtra(BusinessConstants.DATA));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        recyclerViewAdapter = new RecyclerViewAdapter(dataMap);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(recyclerViewAdapter);
        try {
            inventoryPlanId=data.getString(BusinessConstants.ID);
            scanResultCode=data.getString(BusinessConstants.SCAN_RESULT_CODE);
            if(scanResultCode.equals(BusinessConstants.SCANNED)){
                btYes.setTextColor(Color.BLUE);
                btNo.setTextColor(Color.GRAY);
                btJhw.setTextColor(Color.GRAY);
            }else if(scanResultCode.equals(BusinessConstants.UN_SCANNED)){
                btYes.setTextColor(Color.GRAY);
                btNo.setTextColor(Color.BLUE);
                btJhw.setTextColor(Color.GRAY);
            }else {
                btJhw.setTextColor(Color.BLUE);
                btYes.setTextColor(Color.GRAY);
                btNo.setTextColor(Color.GRAY);

            }
            //通过盘点计划id查询已盘点资产信息
            queryCountAssets();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    private static IntentFilter Filter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BusinessConstants.ACTION);
        return intentFilter;
    }

    @Override
    protected void initEvent() {
        //已扫描按钮
        btYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IndexActivity.this, IndexActivity.class);
                Map<String, Object> inventoryMap = new HashMap<>();
                inventoryMap.put(BusinessConstants.ID,inventoryPlanId);
                inventoryMap.put(BusinessConstants.SCAN_RESULT_CODE,BusinessConstants.SCANNED);
                intent.putExtra(BusinessConstants.DATA, JsonUtil.toJSONObject(inventoryMap).toString());
                IndexActivity.this.startActivity(intent);
            }
        });
        //未扫描按钮
        btNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IndexActivity.this, IndexActivity.class);
                Map<String, Object> inventoryMap = new HashMap<>();
                inventoryMap.put(BusinessConstants.ID,inventoryPlanId);
                inventoryMap.put(BusinessConstants.SCAN_RESULT_CODE,BusinessConstants.UN_SCANNED);
                intent.putExtra(BusinessConstants.DATA, JsonUtil.toJSONObject(inventoryMap).toString());
                IndexActivity.this.startActivity(intent);
            }
        });
        btJhw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(IndexActivity.this, IndexActivity.class);
                Map<String, Object> inventoryMap = new HashMap<>();
                inventoryMap.put(BusinessConstants.ID,inventoryPlanId);
                inventoryMap.put(BusinessConstants.SCAN_RESULT_CODE,BusinessConstants.JHW_SCANNED);
                intent.putExtra(BusinessConstants.DATA, JsonUtil.toJSONObject(inventoryMap).toString());
                IndexActivity.this.startActivity(intent);
            }
        });
    }

    @Override
    protected void onSubmit() {

    }

    /**
     * 通过盘点计划id查询已盘点资产信息
     * @return void
     * @author: 黄斌成
     * @Date 2022/12/13 17:11
     **/
    public void queryCountAssets(){
        if(StringUtils.isEmpty(inventoryPlanId)){
            Toast.makeText(IndexActivity.this, "盘点计划id为空", Toast.LENGTH_SHORT).show();
        }
        if(StringUtils.isEmpty(scanResultCode)){
            Toast.makeText(IndexActivity.this, "scanResultCode参数为空", Toast.LENGTH_SHORT).show();
        }
        //JSONObject jsonObject = new JSONObject();
        Map<String, Object> inMap = new HashMap<>();
        Map<String,Object>pageMap=new HashMap<>();
        /*Map<String, Object> orderMap = new HashMap<>();
        orderMap.put("asc",false);
        orderMap.put("column","id");
        List<Map<String, Object>>orderList=new ArrayList<>();
        orderList.add(orderMap);
        pageMap.put("orders",orderList);*/
        pageMap.put("pageIndex",1);
        pageMap.put("pageSize",9999999);
        inMap.put("page",pageMap);
        Map<String,Object>queryMap=new HashMap<>();
        queryMap.put(BusinessConstants.INVENTORY_PLANIID,inventoryPlanId);
        String code = scanResultCode;
        if (BusinessConstants.JHW_SCANNED.equals(scanResultCode)) {
            code = BusinessConstants.SCANNED;
        }
        queryMap.put(BusinessConstants.SCAN_RESULT_CODE,code);
        inMap.put("query",queryMap);
        service.post(JsonUtil.toJSONObject(inMap), HttpUrlComstants.FA_CARD_INVENTORY_DETAIL, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {

                try {
                    JSONArray result = JsonUtil.toJsonObjectArray(response, "content", IndexActivity.this);
                    if (result == null || result.length() == 0) {
                        Toast.makeText(IndexActivity.this, "获取数据为空", Toast.LENGTH_SHORT).show();
                        recyclerView.setBackgroundResource(R.drawable.toolbar_bg_selector);
                        return;
                    }

                    for (int i = 0; i < result.length(); i++) {

                        JSONObject jsonObject = result.getJSONObject(i);
                        String sourceCode = jsonObject.getString("sourceCode");
                        //计划外
                        if (BusinessConstants.JHW_SCANNED.equals(scanResultCode)){
                            if ("scan".equals(sourceCode)) {
                                extracted(jsonObject);
                            }
                        }else {
                            extracted(jsonObject);
                        }

                    }
                    //通知ListView更改数据源
                    if (recyclerViewAdapter != null) {
                        recyclerViewAdapter.notifyDataSetChanged();
                    } else {
                        recyclerViewAdapter = new RecyclerViewAdapter(dataMap);
                        recyclerView.setAdapter(recyclerViewAdapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            private void extracted(JSONObject jsonObject) throws JSONException {
                Map<String,Object> map =  new HashMap<>(3);
                map.put(BusinessConstants.ID, jsonObject.get(BusinessConstants.ID));
                //实物码
                Object cardNumber= jsonObject.get(BusinessConstants.CARD_NUMBER);
                map.put(BusinessConstants.CARD_NUMBER,StringUtils.isEmpty(cardNumber.toString())?"":cardNumber);
                //位置名称
                String locationName= jsonObject.getString(BusinessConstants.LOCATION_NAME);
                map.put(BusinessConstants.LOCATION_NAME,StringUtils.isEmpty(locationName)?"":locationName);
                //计算机名
                Object computerName= jsonObject.get(BusinessConstants.COMPUTER_NAME);
                map.put(BusinessConstants.COMPUTER_NAME,StringUtils.isEmpty(computerName.toString())?"":computerName);
                //使用人
                String usedPsnCode= jsonObject.getString("usedPsnCode");
                String usedPsnName= jsonObject.getString("usedPsnName");
                map.put(BusinessConstants.USED_PSN,StringUtils.isEmpty(usedPsnCode)?"":usedPsnCode+"-"+usedPsnName);
                //资产编码
                Object physicalCode= jsonObject.get(BusinessConstants.PHYSICAL_CODE);
                map.put(BusinessConstants.PHYSICAL_CODE,StringUtils.isEmpty(physicalCode.toString())?"":physicalCode);
                //台账数量
                Object cardNum= jsonObject.get(BusinessConstants.CARD_NUM);
                map.put(BusinessConstants.CARD_NUM,StringUtils.isEmpty(cardNum.toString())?"":cardNum);
                //实盘数量
                Object inventoryNum= jsonObject.get(BusinessConstants.INVENTORY_NUM);
                map.put(BusinessConstants.INVENTORY_NUM,StringUtils.isEmpty(inventoryNum.toString())?"":inventoryNum);
                //物料编号
                Object maNumber= jsonObject.get(BusinessConstants.MA_NUMBER);
                map.put(BusinessConstants.MA_NUMBER,StringUtils.isEmpty(maNumber.toString())?"":maNumber);
                //物料名称
                Object maName= jsonObject.get(BusinessConstants.MA_NAME);
                map.put(BusinessConstants.MA_NAME,StringUtils.isEmpty(maName.toString())?"":maName);
                //异常原因备注
                Object exceptionMemo= jsonObject.get(BusinessConstants.EXCEPTION_MEMO);
                map.put(BusinessConstants.EXCEPTION_MEMO,StringUtils.isEmpty(exceptionMemo.toString())?"":exceptionMemo);
                dataMap.add(map);
                assetIdList.add(jsonObject.get(BusinessConstants.CARD_NUMBER).toString());
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
            }
        });
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.go_back:
                intent = new Intent(IndexActivity.this, com.lyh.hkcloud.module.m1300089.IndexActivity.class);
                IndexActivity.this.startActivity(intent);
                break;
        }
    }

    //时间分发方法重写
    /*@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //如果是点击事件，获取点击的view，并判断是否要收起键盘
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //获取目前得到焦点的view
            View v = getCurrentFocus();
            //判断是否要收起并进行处理
            if (ActivityUtil.isShouldHideKeyboard(v, ev)) {
                ActivityUtil.hideKeyboard(v.getWindowToken(), this, etAssets);
            }
        }
        //这个是activity的事件分发，一定要有，不然就不会有任何的点击事件了
        return super.dispatchTouchEvent(ev);
    }*/
}
